package view.inventory
{
import core.Language;
import core.model.PlayerModel;

import flash.display.MovieClip;

import framework.view.components.button.ButtonProxy;
import framework.view.components.container.IRenderProxy;

import values.avatar.IAvatarValue;
import values.inventory.InventoryValue;
import values.item.EquipmentItemValue;
import values.item.IItemValue;

import view.component.window.PaginationWindowProxy;

/**
 * EquipmentSelectionUIProxy
 * @author jliang
 *
 */
public class EquipmentSelectionUIProxy extends PaginationWindowProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function EquipmentSelectionUIProxy()
	{
		super();
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	[Inject]
	public var playerModel:PlayerModel;
	
	protected var items:Vector.<EquipmentItemValue>;
	
	protected function get okBtn():ButtonProxy
	{
		return proxys["okBtn"];
	}
	
	protected var selectedItem:EquipmentItemValue;
	
	protected var avatar:IAvatarValue;
	
	protected var equipment:EquipmentItemValue;
	
	protected var position:String;
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override protected function get repeaterItemRenderClass():Class
	{
		return EquipmentSelectionItemRenderProxy;
	}
	
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
	}
	
	override protected function clickToGoPrevHandler():void
	{
		updateList(items, items.length, currentPage - 1);
	}
	
	override protected function clickToGoNextHandler():void
	{
		updateList(items, items.length, currentPage + 1);
	}
	
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"okBtn", classRef: ButtonProxy, id:"okBtn", text:Language.getText("@ok_button_label"), onClick:clickToSelectHandler }
		].concat(super.proxyMapping);
	}
	
	override protected function listItemSelectedHandler(render:IRenderProxy):void
	{
		selectedItem = render.data;
		
		okBtn.enabled = true;
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function showEquipment(avatar:IAvatarValue, equipment:EquipmentItemValue):void
	{		
		this.avatar = avatar;
		this.equipment = equipment;
		
		position = equipment.position;
		
		initData();
	}
	
	public function showEquipmentByPosition(avatar:IAvatarValue, position:String):void
	{		
		this.avatar = avatar;
		this.position = position;
		
		initData();
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function initData():void
	{
		items = new Vector.<EquipmentItemValue>();
		
		var ivo:InventoryValue = playerModel.getInventoryValue("equipment");
		for each (var vo:IItemValue in ivo.items)
		{
			if (vo is EquipmentItemValue)
			{
				var item:EquipmentItemValue = vo as EquipmentItemValue;
				if (item.position == position && avatar.avatarType.canSupportEquip(item.equipType))
				{
					if (equipment && item == equipment)
					{
						continue;
					}
					items.push(item);
				}
			}
		}
		
		var pageCount:int = Math.ceil(items.length / preCount);
		
		var page:int = currentPage;
		if (pageCount - 1 < currentPage)
		{
			page = pageCount - 1;
			if (page < 0)
			{
				page = 0;
			}
		}
		updateList(items, items.length, page);
		
		skin.msgLabel.visible = (items.length == 0);
		
		okBtn.enabled = false;
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	protected function clickToSelectHandler():void
	{
		if (selectedItem)
		{
			avatar.addEquipment(selectedItem);
		}		
		this.close();
	}
}
}



